/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controler;

import Model.Pojo.Nhanvien;
import java.awt.Image;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Administrator
 */
public class CXuLy
{

    /**
     * Resize ảnh
     *
     * @param source
     * @param width
     * @param height
     * @return
     */
    public static ImageIcon ReisizeAnh(ImageIcon source, int width, int height)
    {
        Image img = source.getImage();
        Image resizedImage =
                img.getScaledInstance(width, height,
                                      0);
        return new ImageIcon(resizedImage);
    }

    /**
     * Convert mot file bat ky wa byte
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static byte[] ConvertFileToByteArray(String path) throws Exception
    {
        ByteArrayOutputStream bos = null;
        FileInputStream fis = null;
        byte[] temps = new byte[1024];
        int index = 0;

        bos = new ByteArrayOutputStream();
        fis = new FileInputStream(path);
        while (index != -1)
        {

            index = fis.read(temps);

            if (index != -1)
            {
                bos.write(temps, 0, index);
            }
        }
        byte[] imageByte = bos.toByteArray();
        bos.close();
        fis.close();
        return imageByte;

//        String sqlI = "INSERT INTO HocSinh(TenHS,NgaySinh,GhiChu,ExtInfo) values (N'"+jtxtTen.getText()+"','"+jtxtNSinh.getText()+"',N'"+jtxtGhiChu.getText()+"',?)";
//            PreparedStatement ps = Mconn.prepareStatement(sqlI);
//            if(path != null){
//            File FileChon = new File(path);
//            JOptionPane.showMessageDialog(null,path);
//            FileInputStream fs = new FileInputStream(FileChon);
//            ps.setBinaryStream(1,fs,fs.available());
    }

    /**
     * Chon file
     *
     * @return
     */
    public static String ChooseFile()
    {
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        FileFilter imageFilter = null;
        String path = null;

        //Lọc những file hình
        imageFilter = new FileNameExtensionFilter(
                "Image files", ImageIO.getReaderFileSuffixes());
        fc.addChoosableFileFilter(imageFilter);
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            if (fc.getSelectedFile() != null)
            {
                path = fc.getSelectedFile().getAbsolutePath();
                return path;
            }

        }
        return null;
    }

    /**
     * Chuyen doi tu chuoi sang date time
     *
     * @param strdate
     * @return
     * @throws ParseException
     */
    public static Date ConvertStringToDate(String strdate) throws ParseException
    {
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
        return ft.parse(strdate);
    }

    /**
     * Chuyen doi date sang string
     *
     * @param strdate
     * @return
     * @throws ParseException
     */
    public static String ConvertDateToString(Date date) throws ParseException
    {
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
        return ft.format(date);
    }

    /**
     * Dinh danh tien te
     *
     * @param obj
     * @param tiente
     * @return
     */
    public static String FormatTienTe(Object obj, String tiente)
    {
        return String.format("%,.0f " + tiente, obj);
    }

    public static boolean PhanQuyen(HttpServletRequest request, HttpServletResponse response)
    {
        HttpSession session = request.getSession(true);


        if (session.getAttribute("NHAN_VIEN_DANG_NHAP") != null)
        {
            Nhanvien nhanvien;
            nhanvien = (Nhanvien) (session.getAttribute("NHAN_VIEN_DANG_NHAP"));
            String url = request.getServletPath();

            List<String> Customer = new ArrayList<String>();
            Customer.add("/danh-sach-khach-hang.htm");
            Customer.add("/danh-sach-khach-hang.jsp");
            Customer.add("/them-khach-hang.htm");
            Customer.add("/them-khach-hang.jsp");
            Customer.add("/tim-kiem-khach-hang.htm");
            Customer.add("/cap-nhat-thong-tin-khach-hang.htm");

            List<String> Merchandise = new ArrayList<String>();
            Merchandise.add("/danh-sach-san-pham.htm");
            Merchandise.add("/danh-sach-san-pham.jsp");
            Merchandise.add("/them-san-pham.htm");
            Merchandise.add("/them-san-pham.jsp");
            Merchandise.add("/tim-kiem-san-pham.htm");
            Merchandise.add("/cap-nhat-san-pham.htm");

            Merchandise.add("/danh-sach-danh-muc.htm");
            Merchandise.add("/danh-sach-danh-muc.jsp");
            Merchandise.add("/them-danh-muc.htm");
            Merchandise.add("/them-danh-muc.jsp");
            Merchandise.add("/tim-kiem-danh-muc.htm");
            Merchandise.add("/update-danh-muc.htm");
            Merchandise.add("/delete-danh-muc.htm");

            List<String> Accountant = new ArrayList<String>();
            Accountant.add("/danh-sach-don-hang.htm");
            Accountant.add("/danh-sach-don-hang.jsp");
            Accountant.add("/chi-tiet-don-hang.htm");
            Accountant.add("/chi-tiet-don-hang.jsp");
            Accountant.add("/tim-kiem-don-hang.htm");

            switch (nhanvien.getLoainhanvien().getId())
            {
                case 1:
                    try
                    {
                        if (!Customer.contains(url))
                        {
                            response.sendRedirect("danh-sach-khach-hang.htm");
                            return false;
                        }
                        return true;
                    }
                    catch (IOException ex)
                    {
                        Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 2://Merchandise
                    try
                    {
                        if (!Merchandise.contains(url))
                        {
                            response.sendRedirect("danh-sach-san-pham.htm");
                            return false;
                        }
                        return true;
                    }
                    catch (IOException ex)
                    {
                        Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 3://Accountant
                    try
                    {
                        if (!Accountant.contains(url))
                        {
                            response.sendRedirect("danh-sach-don-hang.htm");
                            return false;
                        }
                        return true;
                    }
                    catch (IOException ex)
                    {
                        Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;

                case 4://System Admin
                    return true;
            }
        }
        else
        {
            try
            {
                response.sendRedirect("dang-nhap.htm");
                return false;
            }
            catch (IOException ex)
            {
                Logger.getLogger(DanhSachDonHang.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
}
